<?php
class MysqlModel
{
    private     $db;     //db object
    protected   $table;  //set the current operating data table
    
    public function __construct()
    {
        // create Mysql db object and load the specified section        
        $section = 'run';
        if (Application::getConfig('db_section') != false) {
            $section = Application::getConfig('db_section');
        }
        $this->db = $this->getDb('Mysql', $section);
        
    }
    
    /**
     * Make the Array style fields to sql fields
     *
     * @param  array $_fields
     * 
     * @return string
     */
    protected function getSqlFields(&$_fields)            
    {
        if(is_array($_fields)) {
            return implode(',', $_fields);
        } else {
            return '*';
        }
    }
    
    /**
     * Get a specifiled record from the specifield table.
     * 
     * @param array $_fields
     * @param string $_where the condition of the record
     * 
     * @return array
     */
    public function get($_fields, $_where)
    {
        $fildes = $this->getSqlFields($_fields);
        $sql = "SELECT " . $fildes . " FROM " . $this->table . " WHERE " . $_where;
        return $this->db->getOneRow($sql);
    }
    
    /**
     * Get all or the specifiel records from the specifiel table.
     *
     * @param   array   $_fields    query fields
     * @param   string  $_where
     * @param   string  $_order
     * @param   string  $_limit
     * 
     * @return  array
     */
    public function getList($_fields, $_where = NULL, $_order = NULL, $_limit = NULL)
    {
        $fildes = $this->getSqlFields($_fields);
        $sql = "SELECT " . $fildes . " FROM " . $this->table;
        
        if ($_where != NULL) {
            $sql .= " WHERE " . $_where;
        }
        if ($_order != NULL) {
            $sql .= " ORDER BY " . $_order;
        }
        if ($_limit != NULL) {
            $sql .= " LIMIT " . $_order;
        }
        
        return $this->db->getList($sql);
    }
    
    /**
     * Get the array list grouped by specified column.
     * 
     * @param array $_fields
     * @param string $group_key
     * @param string $_where
     * @param string $_order
     * @param string $_limit
     * 
     * @return multitype:  array |boolean
     */
    public function groupList($_fields, $group_key, $_where = NULL, $_order = NULL, $_limit = NULL)
    {
        if (! in_array($group_key, $_fields)) {
            $_fields = array_push($_fields, $group_key);
        }
        
        $fildes = $this->getSqlFields($_fields);
        $sql = "SELECT " . $fildes . " FROM " . $this->table;
        
        if ($_where != NULL) {
            $sql .= " WHERE " . $_where;
        }
        if ($_order != NULL) {
            $sql .= " ORDER BY " . $_order;
        }
        if ($_limit != NULL) {
            $sql .= " LIMIT " . $_order;
        }
        
        return $this->db->groupList($sql, $group_key);
    }
    
    /**
     * Get the specified col.
     * 
     * @param string $_field, the col name of the table.
     * @param string $_by_key, the result array(if exist) key and it will goup by the key.
     *          for example,
     *          array(
     *              0   => array('name1', 'name2' ...),
     *              1   => 'name3',
     *              2   => array(...)
     *          )
     * @param string $_where
     * 
     * @return bool|array
     */
    public function getCol($_field, $_by_key = NULL, $_where = NULL)
    {
        $sql = "SELECT " . $_field . " FROM " . $this->table;
        
        if ($_where != NULL) {
            $sql .= " WHERE " . $_where;
        }
        
        return $this->db->getCol($this->table, $_field, $_by_key, $_where);
    }
    
    /**
     * Add a new record to the specified db table.
     * 
     * @param  array $_data
     * @return bool
     */
    public function add(&$_data)
    {
        return $this->db->insert($this->table, $_data);
    }
    
    /**
     * Delete all or a specified record from the table.
     * 
     * @param  string $_where
     * 
     * @return bool
     */
    public function delete($_where = NULL)
    {
        return $this->db->delete($this->table, $_where);
    }
    
    /**
     * Update the values the specified record.
     *  
     * @param  array     $_data
     * @param  string    $_where
     * @return bool
     */
    public function update(&$_data, $_where)
    {
        return $this->db->update($this->table, $_data, $_where);
    }
    
    /**
     * Get the total count of the records.
     * 
     * @param   string  $_where
     * @return  integer the total number
     */
    public function total($_where = NULL)
    {
        return $this->db->count($this->table, 0, $_where);
    }
    
    /**
     * Get the specified db instance
     *  It is a Object of class swolf/db/Idb
     *
     * @see ${BASEPATH}/lib/swolf/db/DbFactory
     *
     * @param string $_type (Mysql, Postgresql, Oracle, Mongo)
     * @param string $_key the id of db section
     * 
     * @return  Object
     */
    protected function getDb($_type, $_key)
    {
        Swolf::load('db.DbFactory');
        $_host = Application::getConfig('db');
        return DbFactory::create($_type, $_host[$_key]);
    }
}

